<template>
  <el-dialog
    title="参与详情"
    v-model="visible"
    width="1100px"
    @close="onClose"
    :close-on-click-modal="false"
  >
    <div class="main">
      <div> 活动名称： {{ queryParams.name }} </div>
      <div class="total">
        <div class="item">
          <div class="label">总参与人数</div>
          <div class="value">{{
            Number(totalData.participationClientNum)
              .toString()
              .replace(/\B(?=(\d{3})+(?!\d))/g, ',')
          }}</div>
        </div>
        <div class="item">
          <div class="label">总抽奖次数</div>
          <div class="value">{{
            Number(totalData.drawLotsNum)
              .toString()
              .replace(/\B(?=(\d{3})+(?!\d))/g, ',')
          }}</div>
        </div>
        <div class="item">
          <div class="label">奖品总发放数</div>
          <div class="value">{{
            Number(totalData.provideNum)
              .toString()
              .replace(/\B(?=(\d{3})+(?!\d))/g, ',')
          }}</div>
        </div>
      </div>
      <el-form :model="queryParams" ref="queryRef" :inline="true">
        <el-form-item label="点位名称" prop="positionName">
          <el-input
            style="width: 200px"
            v-model.trim="queryParams.positionName"
            placeholder="请输入"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" size="default" :data="dataList">
        <el-table-column
          label="点位名称"
          align="center"
          prop="positionName"
          show-overflow-tooltip
        />
        <el-table-column label="参与人数" align="center" prop="clientNum" />
        <el-table-column label="抽奖次数" align="center" prop="drawLotsNum" />
        <el-table-column label="奖品核销率" align="center" prop="conversionRate">
          <template #default="{ row }">
            <div>{{ row.conversionRate || '0.00' }}% </div>
          </template>
        </el-table-column>
        <el-table-column label="活动参与率" align="center" prop="participationRate">
          <template #default="{ row }">
            <div>{{ row.participationRate || '0.00' }}% </div>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-right: 10px">
        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
  </el-dialog>
</template>

<script setup>
import { getDetailListApi, getDetailTotalApi } from '@/api/marketing/raffle'
const queryRef = ref(null)

const total = ref(0)
const dataList = ref([])
const visible = ref(false)

const totalData = ref({})

const queryParams = reactive({
  lotteryId: '',
  positionName: '',
  pageNum: 1,
  pageSize: 10
})
const loading = ref(true)

/** 查询用户列表 */
function getList() {
  loading.value = true
  const params = {
    ...queryParams
    // receiveTimeStart: queryParams.time?.length ? queryParams.time[0] : '',
    // receiveTimeEnd: queryParams.time?.length ? queryParams.time[1] : ''
  }
  delete params.time
  delete params.name
  getDetailListApi(params)
    .then((res) => {
      loading.value = false
      dataList.value = res?.data?.rows || []
      total.value = res?.data?.total || 0
    })
    .catch(() => {
      loading.value = false
      dataList.value = []
      total.value = 0
    })
}

function getDetailTotal() {
  getDetailTotalApi({
    lotteryId: queryParams.lotteryId,
    positionName: queryParams.positionName
  }).then((res) => {
    totalData.value = res?.data || {}
  })
}

// 搜索
function handleQuery() {
  queryParams.pageNum = 1
  getList()
  getDetailTotal()
}

// 重置
function resetQuery() {
  queryRef.value.resetFields()
  handleQuery()
}

function onClose() {
  queryRef.value.resetFields()
  queryParams.pageNum = 1
  queryParams.pageSize = 10
}

function init(row) {
  visible.value = true
  console.log('row', row)
  queryParams.lotteryId = row.id
  queryParams.name = row.name
  getList()
  getDetailTotal()
}

defineExpose({
  init
})
</script>

<style lang="scss" scoped>
:deep(.pagination-container .el-pagination) {
  right: 20px;
}
.main {
  position: relative;
  transform: translate3d(0, -30px, 0);
}
.total {
  margin: 10px 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  text-align: center;
  background-color: rgb(219, 222, 224);
  padding: 10px 40px;
  .item {
    .value {
      margin-top: 10px;
    }
  }
}
</style>
